//
//  QMyCustomerVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/16.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QMyCustomerVC.h"
#import "QMyCustomerCell.h"
#import "QCustomerOrderVC.h"
#import "QOrderListModel.h"
#import "QCuOrderModel.h"
@interface QMyCustomerVC ()<UITableViewDataSource,UITableViewDelegate>

{
    NSInteger _index;
    NSMutableArray *_dataArr;
}
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)QOrderListModel *orderListModel;

@end

@implementation QMyCustomerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"客户列表";
    _dataArr = [NSMutableArray array];
    [self createUI];
    [self requestData];
    
    self.tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [self requestData];
    }];
    self.tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [self requestMoreData];
    }];
}

- (void)requestData{
    _index = 1;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMyCommissionOrders"];
    NSDictionary *para =[QParamService getMyCommissionOrdersWithCommissionId:@"-1" Start:start PageSize:KpageSize];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility dismissNoDataViewInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
            
            _orderListModel =[QOrderListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:_orderListModel.orders];
            
            if(_dataArr.count == 0){
                [QViewUtility showNoDataViewInView:_tbView string:@""];
            }else{
                if(_orderListModel.orders.count < 10){
                    [_tbView.mj_footer noticeNothing];
                }
            }
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)requestMoreData{
    _index++;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMyCommissionOrders"];
    NSDictionary *para =[QParamService getMyCommissionOrdersWithCommissionId:@"-1" Start:start PageSize:KpageSize];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            _orderListModel = [QOrderListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:_orderListModel.orders];
            if(_orderListModel.orders.count < 10){
                [_tbView.mj_footer noticeNothing];
                //[QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"没有更多了" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    
}

- (void)createUI{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.tbView registerNib:[UINib nibWithNibName:@"QMyCustomerCell" bundle:nil] forCellReuseIdentifier:@"QMyCustomerCell"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QMyCustomerCell * cell= [tableView dequeueReusableCellWithIdentifier:@"QMyCustomerCell" forIndexPath:indexPath];
    QCuOrderModel * model = _dataArr[indexPath.section];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QCustomerOrderVC * vc =[[QCustomerOrderVC alloc]init];
    QCuOrderModel * model = _dataArr[indexPath.section];
    vc.cuId = model.cusId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
